<?php

namespace App\Http\Controllers;

use Illuminate\Database\Query\Builder;
use Illuminate\Http\Request;
use DB;


class MydbController extends Controller
{
    public function db(Request $request)
    {
        //添加
//        $sql="insert into la_users(username,password)values(:username,:password)";
//        $ret=DB::insert($sql,[':username'=>'admin','password'=>'password']);
//        $sql="insert into la_users(username,password)values(:username,:password)";
//       $ret=DB::insert($sql,['username'=>'admin','password'=>'password']);

//        $sql="update la_users set username=:name where id=:id";
//        $ret=DB::update($Ssql,['username'=>'张三',id=1]);
//        $sql="select  username from la_users  where id=:id";
//        $ret=DB::select($sql,['id'=>1]);
//        $sql="select  * from la_users";
//        $ret=DB::select($sql);


        //关键词搜索
        $kw=$request->get('kw');
//        $ret=DB::table('user')->when($kw,function(Builder $query) use ($kw){
//            $query->where('username','like',"%{$kw}%");
//        } )->get();
//        第二种
//        $ret = DB::table('users')->when($kw,function(Builder $query) use ($kw){
////            $query
//        })
//
//        dump($ret);
    }
    public function db2(){

        //查询所有
//        $ret = DB::table('users')->get();
//        $ret = DB::table('users')->get(['username'])->toArray();
//        $ret=DB::table('users')->where('id','>=',2)->orwhere('username','user1');


//        $ret = DB::table('users')->where('id'=2)->value('username');
//        $ret = DB::table('users')->pluck('username','id');
//        $ret=DB::table('users')->oderby('id','desc')->offset(0)->liit(2)->get();
            $table=DB::table('users');
//            $ret=$table->insert([
//                'username'=>'admin4',
//                'password'=>'user4'
//            ]);
//        $data=[];
//        for($i=5;$i<=10;$i++):
//            $data[]=[
//                'username'=>'admin'.$i,
//               'password'=>'user'.$i
//           ];
//            endfor;
//        $ret=$table->insert($data);

//        $ret = $table->where('id',16)->update([
//            'username'=>'李四'
//        ]);
//        $ret = $table->where('id',19)->delete();
        $ret = $table->delete(15);


        dump($ret);

    }
}
